/*********************************************************************************************************************
 * COPYRIGHT NOTICE
 * Copyright (c) 2018,逐飞科技
 * All rights reserved.
 * 技术讨论QQ群：	一群：179029047(已满)
 *					二群：244861897(已满)
 *					三群：824575535
 *
 * 以下所有内容版权均属逐飞科技所有，未经允许不得用于商业用途，
 * 欢迎各位使用并传播本程序，修改内容时必须保留逐飞科技的版权声明。
 *
 * @file			zf_usart.h
 * @company			成都逐飞科技有限公司
 * @author			逐飞科技(QQ3184284598)
 * @version			查看doc内version文件 版本说明
 * @Software		IAR 8.3 or MDK 5.24
 * @Taobao			https://seekfree.taobao.com/
 * @date			2020-03-25
 * @note			本文件作为 LPC55S 系列芯片开源库外设文件
					提供 USART 外设驱动
 ********************************************************************************************************************/
#ifndef _ZF_USART_H_
#define _ZF_USART_H_

#ifdef RT_THREAD												// 如果启用了 RT-Thread OS
#include <rthw.h>
#include <rtthread.h>
#endif

#include "zf_flexcomm.h"

// *********************************************************** Param define *********************************************************** start
// ========== USART 参数对应选项 ==========
typedef enum
{
	USART_PARAM_BAUD_RATE = 0x00,									// 波特率
	USART_PARAM_PARITY_MODE,										// 校验位
	USART_PARAM_STOP_BITS,											// 停止位
	USART_PARAM_DATA_BITS,											// 数据位
	USART_PARAM_LOOP_BACK,											// 回调调试
	USART_PARAM_RX_FUNC,											// RX 功能
	USART_PARAM_TX_FUNC,											// TX 功能
	USART_PARAM_CONTINUOUS_CLOCK,									// 同步模式时钟输出
	USART_PARAM_TX_FIFO,											// TX FIFO
	USART_PARAM_RX_FIFO,											// RX FIFO
	USART_PARAM_SYNC_MODE,											// 传输模式
	USART_PARAM_CLOCK_POLARITY										// 同步模式时钟极性
}usart_param_t;
// ========== USART 参数对应选项 ==========

// ========== USART 波特率对应选项 ==========
// 兼容的常用波特率 这里详细请参照芯片手册 34.6.6 章节
// 其中对于波特率计算是根据输入到 FlexCOMM 的时钟决定
// input_clock / baudrate = BRGVAL
// 此值用于划分USART输入时钟 以根据来自FRG的输入时钟确定波特率
// 所以部分标准波特率会产生较大的偏差导致无法通讯成功
// 相关波特率偏差标准请自行参照串口传输标准
// Tips : 用户可以自行定义波特率 前提是满足偏差范围
typedef enum
{
	USART_BAUD_RATE_4800 = 4800U,									// 常用波特率 4800U		时钟 12M 下计数值 12000000 / 4800		= 2500
	USART_BAUD_RATE_9600 = 9600U,									// 常用波特率 9600U		时钟 12M 下计数值 12000000 / 9600		= 1250
	USART_BAUD_RATE_19200 = 19200U,									// 常用波特率 19200U		时钟 12M 下计数值 12000000 / 19200	= 625
	USART_BAUD_RATE_115200 = 115200U,								// 常用波特率 115200U	时钟 12M 下计数值 12000000 / 115200	= 104.16~
	USART_BAUD_RATE_230400 = 230400U,								// 常用波特率 230400U	时钟 12M 下计数值 12000000 / 230400	= 52.083~
	USART_BAUD_RATE_460800 = 460800U,								// 常用波特率 460800U	时钟 12M 下计数值 12000000 / 460800	= 26.0416~
}usart_baud_rate_list;
// ========== USART 波特率对应选项 ==========

// ========== USART 检验位对应选项 ==========
typedef enum
{
	USART_PARITY_MODE_DISABLE = 0x00U,								// Parity disabled
	USART_PARITY_MODE_EVEN = 0x02U,									// Parity enabled, type even, bit setting: PE|PT = 10
	USART_PARITY_MODE_ODD = 0x03U,									// Parity enabled, type odd , bit setting: PE|PT = 11
}usart_parity_mode_list;
// ========== USART 检验位对应选项 ==========

// ========== USART 停止位对应选项 ==========
typedef enum
{
	USART_STOP_BITS_1_BIT = 0x00U,									// One stop bit
	USART_STOP_BITS_2_BITS = 0x01U,									// Two stop bits
}usart_stop_bits_list;
// ========== USART 停止位对应选项 ==========

// ========== USART 数据位对应选项 ==========
typedef enum
{
	USART_DATA_BITS_7_BITS = 0x00U,									// 7 bits data lenght
	USART_DATA_BITS_8_BITS = 0x01U,									// 8 bits data lenght
}usart_data_bits_list;
// ========== USART 数据位对应选项 ==========

// ========== USART 调试回调对应选项 ==========
typedef enum
{
	USART_LOOP_BACK_DISABLE = 0x00U,								// Disabled peripheral loopback
	USART_LOOP_BACK_ENABLE = 0x01U,									// Enable peripheral loopback
}usart_loop_back_list;
// ========== USART 调试回调对应选项 ==========

// ========== USART 接收使能对应选项 ==========
typedef enum
{
	USART_RX_FUNC_DISABLE = 0x00U,									// Disabled RX function
	USART_RX_FUNC_ENABLE = 0x01U,									// Enable RX function
}usart_rx_func_list;
// ========== USART 接收使能对应选项 ==========

// ========== USART 发送使能对应选项 ==========
typedef enum
{
	USART_TX_FUNC_DISABLE = 0x00U,									// Disabled TX function
	USART_TX_FUNC_ENABLE = 0x01U,									// Enable TX function
}usart_tx_func_list;
// ========== USART 发送使能对应选项 ==========

// ========== USART 同步模式时钟对应选项 ==========
typedef enum
{
	USART_CONTINUOUS_CLOCK_DISABLE = 0x00U,							// Disabled Continuous SCLK 	Clock on character
	USART_CONTINUOUS_CLOCK_ENABLE = 0x01U,							// Enable Continuous SCLK		Continuous clock
}usart_continuous_clock_list;
// ========== USART 同步模式时钟对应选项 ==========

// ========== USART TX FIFO 对应选项 ==========
typedef enum
{
	USART_TXFIFO_0_ENTRIE  = 0x00U,									// USART tx watermark is empty
	USART_TXFIFO_1_ENTRIE ,											// USART tx watermark at 1 item
	USART_TXFIFO_2_ENTRIES,											// USART tx watermark at 2 item
	USART_TXFIFO_3_ENTRIES,											// USART tx watermark at 3 item
	USART_TXFIFO_4_ENTRIES,											// USART tx watermark at 4 item
	USART_TXFIFO_5_ENTRIES,											// USART tx watermark at 5 item
	USART_TXFIFO_6_ENTRIES,											// USART tx watermark at 6 item
	USART_TXFIFO_7_ENTRIES,											// USART tx watermark at 7 item
	USART_TXFIFO_8_ENTRIES,											// USART tx watermark at 8 item
	USART_TXFIFO_9_ENTRIES,											// USART tx watermark at 9 item
	USART_TXFIFO_10_ENTRIES,										// USART tx watermark at 10 item
	USART_TXFIFO_11_ENTRIES,										// USART tx watermark at 11 item
	USART_TXFIFO_12_ENTRIES,										// USART tx watermark at 12 item
	USART_TXFIFO_13_ENTRIES,										// USART tx watermark at 13 item
	USART_TXFIFO_14_ENTRIES,										// USART tx watermark at 14 item
	USART_TXFIFO_15_ENTRIES,										// USART tx watermark at 15 item
}usart_txfifo_list;
// ========== USART TX FIFO 对应选项 ==========

// ========== USART RX FIFO 对应选项 ==========
typedef enum
{
	USART_RXFIFO_1_ENTRIE  = 0x00U,									// USART rx watermark at 1 item
	USART_RXFIFO_2_ENTRIES,											// USART tx watermark at 2 item
	USART_RXFIFO_3_ENTRIES,											// USART tx watermark at 3 item
	USART_RXFIFO_4_ENTRIES,											// USART tx watermark at 4 item
	USART_RXFIFO_5_ENTRIES,											// USART tx watermark at 5 item
	USART_RXFIFO_6_ENTRIES,											// USART tx watermark at 6 item
	USART_RXFIFO_7_ENTRIES,											// USART tx watermark at 7 item
	USART_RXFIFO_8_ENTRIES,											// USART tx watermark at 8 item
	USART_RXFIFO_9_ENTRIES,											// USART tx watermark at 9 item
	USART_RXFIFO_10_ENTRIES,										// USART tx watermark at 10 item
	USART_RXFIFO_11_ENTRIES,										// USART tx watermark at 11 item
	USART_RXFIFO_12_ENTRIES,										// USART tx watermark at 12 item
	USART_RXFIFO_13_ENTRIES,										// USART tx watermark at 13 item
	USART_RXFIFO_14_ENTRIES,										// USART tx watermark at 14 item
	USART_RXFIFO_15_ENTRIES,										// USART tx watermark at 15 item
	USART_RXFIFO_16_ENTRIES,										// USART tx watermark at 16 item
}usart_rxfifo_list;
// ========== USART RX FIFO 对应选项 ==========

// ========== USART 传输模式对应选项 ==========
typedef enum
{
	USART_SYNC_MODE_DISABLE = 0x00U,								// Asynchronous mode.
	USART_SYNC_MODE_SLAVE = 0x02U,									// Synchronous slave mode.
	USART_SYNC_MODE_MASTER = 0x03U,									// Synchronous master mode.
}usart_sync_mode_list;
// ========== USART 传输模式对应选项 ==========

// ========== USART 同步模式时钟属性对应选项 ==========
typedef enum
{
	USART_CLOCK_POLARITY_FALLING_EDGE = 0x00U,						// Un_RXD is sampled on the falling edge of SCLK
	USART_CLOCK_POLARITY_RISIG_EDGE = 0x01U,						// Un_RXD is sampled on the rising edge of SCLK
}usart_clock_polarity_list;
// ========== USART 同步模式时钟属性对应选项 ==========

// ========== USART 中断使能对应选项 ==========
typedef enum
{
	USART_INTERRUPT_MODE_TXERR = (0x01U),							// 中断使能 TXERR 中断允许
	USART_INTERRUPT_MODE_RXERR = (0x02U),							// 中断使能 RXERR 中断允许
	USART_INTERRUPT_MODE_TXLVL = (0x04U),							// 中断使能 TXLVL 中断允许
	USART_INTERRUPT_MODE_RXLVL = (0x08U),							// 中断使能 RXLVL 中断允许
}usart_interrupt_mode_list;
// ========== USART 中断使能对应选项 ==========

// ========== USART FIFO 状态及对应选项 ==========
typedef enum
{
	USART_FIFO_STATUS_TXERR = (0x00000001U),						// FIFO 状态 TXERR bit 0
	USART_FIFO_STATUS_RXERR = (0x00000002U),						// FIFO 状态 RXERR bit 1
	USART_FIFO_STATUS_PERINT = (0x00000008U),						// FIFO 状态 PERINT bit 3
	USART_FIFO_STATUS_TXEMPTY = (0x00000010U),						// FIFO 状态 TXEMPTY bit 4
	USART_FIFO_STATUS_TXNOTFULL = (0x00000020U),					// FIFO 状态 TXNOTFULL bit 5
	USART_FIFO_STATUS_RXNOTEMPTY = (0x00000040U),					// FIFO 状态 RXNOTEMPTY bit 6
	USART_FIFO_STATUS_RXFULL = (0x00000080U),						// FIFO 状态 RXFULL bit 7
}usart_fifo_status_list;
// ========== USART FIFO 状态及对应选项 ==========

// ========== USART FIFO 设置及对应选项 ==========
typedef enum
{
	USART_FIFO_CFG_ENABLETX = (0x00000001U),
	USART_FIFO_CFG_ENABLERX = (0x00000002U),
	USART_FIFO_CFG_DMATX = (0x00001000U),
	USART_FIFO_CFG_DMARX = (0x00002000U),
	USART_FIFO_CFG_WAKETX = (0x00004000U),
	USART_FIFO_CFG_WAKERX = (0x00008000U),
	USART_FIFO_CFG_EMPTYTX = (0x00010000U),
	USART_FIFO_CFG_EMPTYRX = (0x00020000U),
}usart_fifo_cfg_list;
// ========== USART FIFO 设置及对应选项 ==========
// *********************************************************** Param define *********************************************************** end

extern usart_handle_t g_uartHandle[8];

// ******************************************************** Function statement ******************************************************** start
//================== Flex COMM USART config ==================
void		zf_usart_get_default_config			(usart_config_t *config);
void		zf_usart_param_set 					(usart_config_t *config, usart_param_t param_index, uint32_t data);
void		zf_usart_transfer_great_handle		(flexcomm_index_list index, usart_transfer_callback_t callback);
void		zf_usart_interrupt_enable			(flexcomm_index_list index, void handler(void));
void		zf_usart_interrupt_disable			(flexcomm_index_list index);
void		zf_usart_interrupt_mode_set			(flexcomm_index_list index, uint8_t interrput_mode);
//================== Flex COMM USART config ==================

//================== Flex COMM USART status ==================
uint32_t	zf_usart_get_fifo_status			(flexcomm_index_list index);
uint32_t	zf_usart_get_fifo_config			(flexcomm_index_list index);
void		zf_usart_set_fifo_status			(flexcomm_index_list index, uint32_t mask);
void		zf_usart_set_fifo_config			(flexcomm_index_list index, uint32_t mask);
uint8_t		zf_usart_get_fifo_TXLVL				(flexcomm_index_list index);
uint8_t		zf_usart_get_fifo_RXLVL				(flexcomm_index_list index);
//================== Flex COMM USART status ==================

//================== Flex COMM USART function ==================
void		zf_usart_write_byte					(flexcomm_index_list index, uint8_t data);
uint8_t		zf_usart_read_byte					(flexcomm_index_list index);
size_t		zf_usart_write_blocking				(flexcomm_index_list index, uint8_t *data, size_t length);
size_t		zf_usart_read_blocking				(flexcomm_index_list index, uint8_t *data, size_t length);
void		zf_usart_transfer_write				(flexcomm_index_list index, uint8_t *data, size_t length);
void		zf_usart_transfer_read				(flexcomm_index_list index, uint8_t *data, size_t length);
//================== Flex COMM USART function ==================

// ******************************************************** Function statement ******************************************************** end

#endif
